package com.moxiao.yfjh.module.blind.controller.app.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundExportReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.background.BackgroundDO;
import com.moxiao.yfjh.module.blind.service.background.BackgroundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 背景图管理")
@RestController
@RequestMapping("/blind/background")
@Validated
public class AppBackgroundController {

    @Resource
    private BackgroundService backgroundService;

    @GetMapping("/getBackground")
    @Operation(summary = "获得背景图")
    public CommonResult<JSONObject> getBackgroundPage() {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<BackgroundDO> backgroundList = backgroundService.getBackgroundList(new BackgroundExportReqVO());
        JSONObject jsonObject = new JSONObject();
//        1. 获取官方活动
        List<BackgroundDO> collect = backgroundList.stream().filter(s -> s.getType().equals("1")).filter(s -> s.getStatus().equals(0)).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collect)) {
            if (CollectionUtils.isNotEmpty(collect)) {
//            1.如果只有一张
                if (1 == backgroundList.size()) {
                    BackgroundDO backgroundDO = backgroundList.stream().findFirst().orElse(null);
                    jsonObject.put("officialActivity", backgroundDO.getUrl());
                } else {
//                随机一张
                    Random random = new Random();
                    int randomIndex = random.nextInt(backgroundList.size());
                    BackgroundDO backgroundDO = backgroundList.get(randomIndex);
                    jsonObject.put("officialActivity", backgroundDO.getUrl());
                }
                jsonObjectList.add(jsonObject);
            } else {
                jsonObject.put("officialActivity", null);
                jsonObject.put("singleActivity", null);
                jsonObjectList.add(jsonObject);
            }
        } else {
            jsonObject.put("officialActivity", null);
            jsonObject.put("singleActivity", null);
            jsonObjectList.add(jsonObject);
        }

        List<BackgroundDO> collectSingle = backgroundList.stream().filter(s -> s.getType().equals("2")).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collectSingle)) {
//            1.如果只有一张
            if (1 == collectSingle.size()) {
                BackgroundDO backgroundDO = collectSingle.stream().findFirst().orElse(null);
                jsonObject.put("singleActivity", backgroundDO.getUrl());
            } else {
//                随机一张
                Random random = new Random();
                int randomIndex = random.nextInt(collectSingle.size());
                BackgroundDO backgroundDO = collectSingle.get(randomIndex);
                jsonObject.put("singleActivity", backgroundDO.getUrl());
            }
            jsonObjectList.add(jsonObject);
        } else {
            jsonObject.put("officialActivity", null);
            jsonObject.put("singleActivity", null);
            jsonObjectList.add(jsonObject);
        }
        return success(jsonObject);
    }

}
